<?php
namespace App\Http\Controllers\Backstage;
use App\Http\Controllers\Controller;
use App\Model\Backstage\User;
use App\Tool\Error;
use Illuminate\Http\Request;
use App\Model\Backstage\Achievement;
use App\Model\Backstage\RoomInfo;
use App\Model\Backstage\Customers;
use App\Model\Backstage\RoomFollow;
use App\Model\Backstage\CustomersFollow;
/**
 * Class AchievementHouseController
 * @package App\Http\Controllers\Backstage
 * 业绩排行
 * date:2018-6-09
 * author:lxy
 */
class AchievementController extends Controller
{

    protected $achievement_model;  //业绩model
    protected $roomInfo_model;    //房源model
    protected $customers_model;  //客源model
    protected $roomFollow_model;  //房源跟进model
    protected $customersFollow_model; //客源跟进model
    protected $begin_month_time;  //当前月份开始时间
    public function __construct()
    {
        $this->begin_month_time = mktime ( 0, 0, 0, date ( "m" ), 1, date ( "Y" ) ) ;  //当月开始时间戳
        $this->achievement_model     = new Achievement();
        $this->roomInfo_model        = new RoomInfo();
        $this->customers_model       = new Customers();
        $this->roomFollow_model      = new RoomFollow();
        $this->customersFollow_model = new CustomersFollow();
    }

    /**
     * 获取业绩排行统计
     */
    public function getAchievementList(Request $request){
        $param = $request->all();
        $map = [];
        if(isset($param['begin_time']) && $param['begin_time'] != ''){
            $map[] = ['a_created_at', '>=',strtotime($param['begin_time'])];
        }
        if(isset($param['end_time']) && $param['end_time'] != ''){
            $map[] = ['a_created_at', '<=',strtotime($param['end_time'])];
        }
        if(empty($map)){
            $map[] = ['a_created_at', '>=',$this->begin_month_time];
            $map[] = ['a_created_at', '<=',time()];
        }
        $res = $this->achievement_model->getAchievementList($map,env('PAGE_LIMIT_ADMIN'));
        if (!$res) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $res);
    }

    /**
     * 获取员工录入房源排行
     */
    public function getRoomInfoAchievementList(Request $request){
        $param = $request->all();
        $map = [];
        if(isset($param['begin_time']) && $param['begin_time'] != ''){
            $map[] = ['r_created_at', '>=',strtotime($param['begin_time'])];
        }
        if(isset($param['end_time']) && $param['end_time'] != ''){
            $map[] = ['r_created_at', '<=',strtotime($param['end_time'])];
        }
        if(empty($map)){
            $map[] = ['r_created_at', '>=',$this->begin_month_time];
            $map[] = ['r_created_at', '<=',time()];
        }
        $res = $this->roomInfo_model->roomAchievementList($map,env('PAGE_LIMIT_ADMIN'));
        if (!$res) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $res);
    }


    /**
     * @param Request $request
     * 获取员工录入客源信息排行榜
     */
    public function getCustomerInfoAchievementList(Request $request){
        $param = $request->all();
        $map = [];
        if(isset($param['begin_time']) && $param['begin_time'] != ''){
            $map[] = ['c_created_at', '>=',strtotime($param['begin_time'])];
        }
        if(isset($param['end_time']) && $param['end_time'] != ''){
            $map[] = ['c_created_at', '<=',strtotime($param['end_time'])];
        }
        if(empty($map)){
            $map[] = ['c_created_at', '>=',$this->begin_month_time];
            $map[] = ['c_created_at', '<=',time()];
        }
        $res = $this->customers_model->customersAchievementList($map,env('PAGE_LIMIT_ADMIN'));
        if (!$res) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $res);
    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取员工房源跟进记录排行
     */
    public function getRoomFollowNum(Request $request){
        $param = $request->all();
        $map = [];
        if(isset($param['begin_time']) && $param['begin_time'] != ''){
            $map[] = ['f_created_at', '>=',strtotime($param['begin_time'])];
        }
        if(isset($param['end_time']) && $param['end_time'] != ''){
            $map[] = ['f_created_at', '<=',strtotime($param['end_time'])];
        }
        if(empty($map)){
            $map[] = ['f_created_at', '>=',$this->begin_month_time];
            $map[] = ['f_created_at', '<=',time()];
        }
        $res = $this->roomFollow_model->getRoomFollowNum($map,env('PAGE_LIMIT_ADMIN'));
        if (!$res) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $res);
    }



    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取员工客源跟进记录排行
     */
    public function getCustomersFollowNum(Request $request){
        $param = $request->all();
        $map = [];
        if(isset($param['begin_time']) && $param['begin_time'] != ''){
            $map[] = ['cf_created_at', '>=',strtotime($param['begin_time'])];
        }
        if(isset($param['end_time']) && $param['end_time'] != ''){
            $map[] = ['cf_created_at', '<=',strtotime($param['end_time'])];
        }
        if(empty($map)){
            $map[] = ['cf_created_at', '>=',$this->begin_month_time];
            $map[] = ['cf_created_at', '<=',time()];
        }
        $res = $this->customersFollow_model->getCustomersFollowNum($map,env('PAGE_LIMIT_ADMIN'));
        if (!$res) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $res);
    }

}